/*
Automatic clinic tests using a robot arm.
Copyright (C) 2010  CIC-CPC, CHU Gabriel Montpied
All other trademarks, logos and copyrights are the property
of their respective owners.

This file is part of cpc-painbot.

cpc-painbot is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

cpc-painbot is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with cpc-painbot.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "assistantwindup.h"
#include "ui_assistantwindup.h"

#include "operation/etalonnageeva.h"
#include "operation/configurertrain.h"
#include "operation/choixdetermination.h"

#include "seuil/determination.h"
#include "seuil/methode.h"
#include "seuil/methode/windup.h"

#include "indicateurprocess.h"
#include "capteur/eva.h"

#include "windupdialog.h"

AssistantWindUp::AssistantWindUp(Bras *bras, IndicateurProcess *indicateur, QWidget *parent) :
    QWizard(parent),
    ui(new Ui::AssistantWindUp),
    indicateur_(indicateur) // Temporaire, à retirer.
{
    ui->setupUi(this);

    //! \todo Création du Widget WindUp.
    indicateur->setCapteur(new capteur::Eva(indicateur));

    //! \todo Plus de libertés quant au choix du stimulateur.
    addPage(new ChoixDetermination(this));
    addPage(new EtalonnageEva(indicateur, this));
    addPage(new ConfigurerTrain(this));

    //! \todo Page récapitulatif Wind Up.
    //addPage(new RecapitulatifWindUp(this));

    // Visuels
    setPixmap(LogoPixmap, QPixmap(":/images/cic-logo-alt"));

}

AssistantWindUp::~AssistantWindUp()
{
    delete ui;
}

void AssistantWindUp::accept()
{
    WindUpDialog *dialog = new WindUpDialog(this);


    // Paramétrage de la Détermination.
    seuil::Determination *determination = dialog->determination();
    determination->setType(seuil::Determination::Type(field("determination").toInt()));

    // Paramétrage de la méthode WindUp du WindUpDialog.
    seuil::methode::WindUp *windup = qobject_cast<seuil::methode::WindUp*>(determination->methode());

    // Connection des signaux de la méthode
    connect(windup, SIGNAL(frequenceChangee(int)),  dialog, SLOT(setFrequence(int)));
    connect(windup, SIGNAL(totalChange(int)),       dialog, SLOT(setTotal(int)));
    connect(windup, SIGNAL(restantChange(int)),     dialog, SLOT(setRestant(int)));
    connect(windup, SIGNAL(termineeChange(bool)),   dialog, SLOT(setTerminer(bool)));

    windup->setFrequence(field("train.frequence").toInt());
    windup->setTotal(field("train.nombre").toInt());


    // Paramétrage du stimulateur.
    Stimulateur *stimulateur = Stimulateur::creer(Stimulateur::Type(field("stimulateur").toInt()));
    determination->setStimulateur(stimulateur);

    // Connection du signal issus du capteur.
    //! \todo Cette connexion devrait se faire en dehors.
    indicateur_->arreter();
    indicateur_->setIntervalle(windup->frequence());
    connect(indicateur_->capteur(), SIGNAL(mesure(QVariant)), dialog, SLOT(setDouleur(QVariant)));
    connect(indicateur_->capteur(), SIGNAL(mesure(QVariant)), windup, SLOT(douleur(QVariant)));

    // Lancement de la détermination.
    // La synchronisation se fait grâce à deux timers en singleshot.
    // Le premier va lancer le stimulateur après un léger délai,
    // tandis que le second va lancer la prise des mesures à contre-temps.
    int frequence = field("frequence").toInt();
    QTimer::singleShot(frequence, stimulateur, SLOT(stimuler()));
    QTimer::singleShot(qRound(frequence + qreal(frequence)/2), indicateur_, SLOT(demarrer()));

    // Affichage du Dialog une fois l'assistant terminé.
    connect(this, SIGNAL(accepted()), dialog, SLOT(show()));
    QWizard::accept();
}

void AssistantWindUp::changeEvent(QEvent *e)
{
    QWizard::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}
